<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="24">
          <!-- 填报主题 -->
          <el-form-item label="填报主题" prop="topicId">
            <!--文本框-->
            <el-select v-model="dataForm.topicId" filterable placeholder="填报主题" clearable
              @change="topicChangeHandle(dataForm.topicId)" style="width:100%">
              <el-option v-for="item in topicList" :key="item.id" :label="item.title" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 填报单位 -->
          <el-form-item label="填报单位" prop="btypeId">
            <!--文本框-->
            <el-input v-model="dataForm.btypeId" placeholder="填报单位" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 填报标题 - 自定义 -->
          <el-form-item label="填报标题" prop="fillTitle">
            <!--文本框-->
            <el-input v-model="dataForm.fillTitle" placeholder="填报标题" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 周期类型 -->
          <el-form-item label="周期类型" prop="cycleType">
            <!--文本框-->
            <el-input v-model="dataForm.cycleType" placeholder="周期类型" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 填报年度 -->
          <el-form-item label="填报年度" prop="fillYear">
            <!--文本框-->
            <el-input v-model="dataForm.fillYear" placeholder="填报年度" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 填报周期 -->
          <el-form-item label="填报周期" prop="fillCycle">
            <!--文本框-->
            <el-input v-model="dataForm.fillCycle" placeholder="填报周期" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 填报日期 -->
          <el-form-item label="填报日期" prop="fillDate">
            <!--日期框-->
            <el-date-picker v-model="dataForm.fillDate" value-format="yyyy-MM-dd HH:mm:ss" placeholder="填报日期"
              type="date"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 备注说明 -->
          <el-form-item label="备注说明" prop="remark">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="备注说明"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      topicList: [],
      dataForm: {
        id: '',
        topicId: '',
        btypeId: '',
        cycleType: '',
        fillYear: '',
        fillCycle: '',
        fillTitle: '',
        fillDate: '',
        remark: '',
        status: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
        this.getTopicList()
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/udf/udfFillingLog/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 获取主题
    getTopicList () {
      this.$http.get(`/udf/udfFillingTopic/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.topicList = res.data
      }).catch(() => { })
    },
    topicChangeHandle (topicId) {
      let topic = this.$getDataItem(this.topicList, topicId, 'id')
      this.dataForm.startTime = topic.startTime
      this.dataForm.endTime = topic.endTime
      this.dataForm.unit = topic.unit
      this.dataForm.typeDirection = topic.typeDirection
      this.dataForm.cycleType = topic.cycleType
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/udf/udfFillingLog/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
